//
//  DatePickerView.swift
//  zob
//
//  Created by A-Jay on 1/4/2017.
//  Copyright © 2017年 A-Jay. All rights reserved.
//

import UIKit

class DatePickerView: UIView {
    
    //block
    var dateBlock:((String, Date?) -> ())?
    
    
    
    let datePickerHeight : CGFloat = 216.0;
    let buttonViewHeight : CGFloat = 40.0;
    let buttonWidth : CGFloat = 80.0;
    
    
    //MARK: - Getters And Setters ( getter 和 setter  )
    
    private lazy var bgView:UIView = {
        let view = UIView()
        self.addSubview(view)
        return view
    }()
    
    private lazy var buttonBGView:UIView  = {
        let view = UIView()
        view.layer.borderColor = UIColor.hexStringToColor(hexString: "#919191").cgColor;
        view.layer.borderWidth = 0.5;
        self.bgView.addSubview(view)
        return view
    }()
    
    private lazy var canCelButton:UIButton = {
        let button = UIButton.creatTitleButton("取消", "#e3483d", 14)
        button.addTarget(self, action: #selector(clickCancelButton(sender:)), for: .touchUpInside)
        self.buttonBGView.addSubview(button)
        return button;
    }()
    
    private lazy var doneButton:UIButton = {
        let button = UIButton.creatTitleButton("完成", "#e3483d", 14)
        button.addTarget(self, action: #selector(clickDoneButton(sender:)), for: .touchUpInside)
        self.buttonBGView.addSubview(button)
        return button;
    }()
    
    private lazy var datePicker:UIDatePicker = {
        let picker = UIDatePicker()
        picker.locale = Locale(identifier: "zh_CN")                 //将日期选择器区域设置为中文，则选择器日期显示为中文
        picker.datePickerMode = .date                               //选择模式
//        picker.setValue(UIColor.white, forKey: "textColor")         //修改文字颜色
//        picker.addTarget(self, action: #selector(dateChanged(datePicker:)),
//                             for: .valueChanged)
        self.bgView.addSubview(picker)
        return picker
    }()
    
    
    //MARK: - Life Cycle ( 生命周期方法 )
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame:CGRect){
        super.init(frame: frame);
        
        self.bgView.frame = CGRect(x: 0, y: self.bounds.size.height - datePickerHeight - buttonViewHeight, width: UIScreen.main.bounds.size.width, height: datePickerHeight + buttonViewHeight)
        self.buttonBGView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: buttonViewHeight)
        self.canCelButton.frame = CGRect(x: 0, y: 0, width: buttonWidth, height: buttonViewHeight)
        self.doneButton.frame = CGRect(x: UIScreen.main.bounds.size.width - buttonWidth, y: 0, width: buttonWidth, height: buttonViewHeight)
        self.datePicker.frame = CGRect(x: 0, y: buttonViewHeight, width: UIScreen.main.bounds.size.width, height: datePickerHeight)
        
    }
    
    //MARK: - Private Methods ( 私有方法 )
    
    
    //MARK: - Public Methods ( 公有方法 )
    
    
    //MARK: - Event Responses ( 事件响应 )
    //日期选择器响应方法
//    func dateChanged(datePicker : UIDatePicker){
//        //更新提醒时间文本框
//        let formatter = DateFormatter()
//        //日期样式
//        formatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
//        date = formatter.string(from: datePicker.date)
//        print(date)
//    }
    
    func clickCancelButton(sender: UIButton) {
        dateBlock!("", nil)
    }
    
    func clickDoneButton(sender: UIButton) {
        //更新提醒时间文本框
        let formatter = DateFormatter()
        //日期样式
        formatter.dateFormat = "yyyy-MM-dd"
        dateBlock!(formatter.string(from: datePicker.date),datePicker.date)
    }
    
    
    //MARK: - "控件代理方法"
    
    //MARK: - Notification Responses ( 通知响应 )
    
    
    //MARK: - Network Data ( 网络请求处理 )
    
    
    

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
